package dao.impl;

import bean.Menu;
import bean.Role;
import dao.RoleDao;
import util.DBUtil;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DBUtil implements RoleDao {

    /**
     * 用于用户获取角色列表信息
     * @return
     */
    @Override
    public List<Role> getList() {

        List<Role> roleList = new ArrayList<>();

        try {
            String sql = " select * from role ";

            resultSet = query(sql);

            while (resultSet.next()){

                Role role =  new Role();

                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                roleList.add(role);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return roleList;
    }




    /**
     * 角色管理功能模块；获取角色的所有信息
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<Role> getRoleList(int pageIndex, Integer pageSize) {

        List<Role> roleList = new ArrayList<>();

        try {
            //数据分页查询，显示
            String sql = " select * from role limit ?,? ";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);

            resultSet = query(sql,params);

            while (resultSet.next()){

                Role role =  new Role();

                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                roleList.add(role);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return roleList;
    }



    @Override
    public int total() {
        int total = 0;

        try {
            //数据分页查询，显示
            String sql = " select count(*) from role ";

            resultSet = query(sql);

            while (resultSet.next()){

                total = resultSet.getInt(1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return total;
    }


    /**
     * 新增角色
     * @param role
     * @return
     */
    @Override
    public int insert(Role role) {

        int newRoleId = 0; //新增数据的id

        try {
            String sql = " insert into role values(null,?,?) ";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());

            //update保存受影响的行数
            int update = update(sql,params);

            ResultSet generatedKeys = pps.getGeneratedKeys();

            if (generatedKeys.next()){
                newRoleId = generatedKeys.getInt(1);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return newRoleId;
    }

    /**
     * 通过角色id获取角色信息
     * @param roleId
     * @return
     */
    @Override
    public Role findById(int roleId) {

        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();

        try {
            String sql = " select * from role r,menu m,middle mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=? ";
            List params = new ArrayList();
            params.add(roleId);

            resultSet = query(sql,params);
            while (resultSet.next()){

                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setUpMenuId(resultSet.getInt("upmenuid"));
                menuList.add(menu);

            }
            role.setMenuList(menuList);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return role;
    }

    /**
     * 通过角色id删除角色信息
     * @param roleId
     * @return
     */
    @Override
    public int delete(int roleId) {

        int i = 0;

        try {
            String sql = " delete from role where roleid=? ";
            List params = new ArrayList();

            params.add(roleId);

            i = update(sql,params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return i;
    }

    @Override
    public int update(Role role) {
        int i = 0;

        try {
            String sql = " update role set rolename=?,rolestate=? where roleid=? ";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            params.add(role.getRoleId());

            i = update(sql,params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return i;
    }


}
